use std::path::Path;
use std::fs::File;
use anyhow::Result;
use ark_bn254::{Bn254, Fr,Parameters};
use ark_groth16::{generate_random_parameters, create_random_proof, Proof, ProvingKey};
use ark_serialize::CanonicalSerialize;
use rand::thread_rng;
use chrono::Utc;
use chrono::Datelike;
use crate::circuit::age_range::AgeRangeCircuit;
use ark_serialize::CanonicalDeserialize; 






pub struct ProofGenerator {
    //params: ProvingKey<Bn<Parameters>>,
    pk: ProvingKey<Bn254>,
}

impl ProofGenerator {
    pub fn new(circuit_path: &Path) -> Result<Self> {
        if !circuit_path.exists() {
            return Err(anyhow::anyhow!("电路参数文件不存在: {:?}", circuit_path));
        }
        let params = ProvingKey::<Bn254>::deserialize(
            &mut File::open(circuit_path)?,
        )?;

        //let pk = params.proving_key().clone();
        
        Ok(Self { /* params, */ pk:params })
    }
    
    pub fn generate_proof(
        &self,
        birth_year: u32,
        min_age: u32,
        max_age: u32,
    ) -> Result<Proof<Bn254>> {
        let circuit = AgeRangeCircuit {
            birth_year: Fr::from(birth_year as u64),
            min_age: Fr::from(min_age as u64),
            max_age: Fr::from(max_age as u64),
            current_year: Fr::from(Utc::now().year() as u64),
        };
        
        let proof = create_random_proof(
            circuit,
            &self.pk,
            &mut thread_rng(),
        )?;
        
        Ok(proof)
    }
} 